package com.chuangsi.common.utils;


import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.time.temporal.TemporalAdjusters;
import java.time.temporal.TemporalUnit;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

public class LocalDateTimeUtils {

    public static final String YYYYMMDDHHMMSSCHINESE="yyyy年MM月dd日HH时mm分ss秒";
    public static final String YYYYMMDDHHMMSS="yyyy-MM-dd HH:mm:ss";
    public static final String YYMMDDHHMM="yy-MM-dd HH:mm";
    public static final String YYYYMMDD="yyyy-MM-dd";
    public static final String YYYYMMDDCHINESE="yyyy年MM月dd日";
    public static final String MMDDCHINESE="MM月dd日";
    public static final String YYYYMM="yyyy-MM";
    public static final String YYYY="yyyy";


    public static LocalDateTime getLocalDateTimeOfTimestamp(Long timestamp) {
        if(timestamp==null){
            return null;
        }
        Instant instant = Instant.ofEpochMilli(timestamp);
        ZoneId zone = ZoneId.systemDefault();
        return LocalDateTime.ofInstant(instant, zone);
    }

    // 判定token是否过期
    public static boolean compareDate(LocalDateTime datetime,Integer minute){
        boolean f = false;
        LocalDateTime newDate = datetime.plusMinutes(minute);
        LocalDateTime nowDate = LocalDateTime.now();
        if(newDate.isAfter(nowDate)){
            f = true;
        }
        return f;
    }

    // 判定token是否过期
    public static boolean compareReDate(LocalDateTime datetime,Integer minute){
        boolean f = false;
        LocalDateTime newDate = datetime.plusMinutes(minute);
        LocalDateTime nowDate = LocalDateTime.now();
        if(newDate.isBefore(nowDate)){
            f = true;
        }
        return f;
    }

    //Date转换为LocalDateTime
    public static LocalDateTime convertDateToLDT(Date date) {
        return LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault());
    }

    //LocalDateTime转换为Date
    public static Date convertLDTToDate(LocalDateTime time) {
        return Date.from(time.atZone(ZoneId.systemDefault()).toInstant());
    }


    //获取指定日期的毫秒
    public static Long getMilliByTime(LocalDateTime time) {
        return time.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli();
    }

    //获取指定日期的秒
    public static Long getSecondsByTime(LocalDateTime time) {
        return time.atZone(ZoneId.systemDefault()).toInstant().getEpochSecond();
    }

    //获取指定时间的指定格式
    public static String formatTime(LocalDateTime time,String pattern) {
        if(time==null)return "";
        return time.format(DateTimeFormatter.ofPattern(pattern));
    }
    //获取指定时间的指定格式
    public static String formatTime(LocalDate time,String pattern) {
        if(time==null)return "";
        return time.format(DateTimeFormatter.ofPattern(pattern));
    }
    //获取指定时间的指定格式
    public static String formatTime(LocalDate time) {
        if(time==null)return "";
        return time.format(DateTimeFormatter.ofPattern(YYYYMMDD));
    }

    //获取指定时间的指定格式
    public static String formatTime(LocalDateTime time) {
        if(time == null){
            return "";
        }
        return time.format(DateTimeFormatter.ofPattern(YYYYMMDDHHMMSS));
    }
    //格式化
    public static LocalDateTime formatTime(String time) {
        DateTimeFormatter formatter2 = DateTimeFormatter.ofPattern(YYYYMMDDHHMMSS);
        LocalDateTime parsedDateTime2 = LocalDateTime.parse(time, formatter2);
        return parsedDateTime2;
    }
    //获取指定时间的指定格式
    public static LocalDate formatLocalDate(String time) {
        if(StringUtils.isBlank(time)){
            return null;
        }
        DateTimeFormatter formatter2 = DateTimeFormatter.ofPattern(YYYYMMDD);
        LocalDate parsedDate2 = LocalDate.parse(time, formatter2);
        return parsedDate2;
    }



    //获取指定时间的指定格式
    public static LocalDateTime formatLocalDateTime(String time,String pattern) {
        if(StringUtils.isBlank(pattern))pattern = YYYYMMDDHHMMSS;
        try {
            DateTimeFormatter formatter2 = DateTimeFormatter.ofPattern(pattern);
            return LocalDateTime.parse(time,formatter2);
        }catch (Exception e){
            return LocalDateTime.parse(time+" 00:00:00",DateTimeFormatter.ofPattern(pattern));
        }
    }


    //获取当前时间的指定格式
    public static String formatNow(String pattern) {
        return  formatTime(LocalDateTime.now(), pattern);
    }

    //获取指定时间的指定格式
    public static LocalDate formatTime(String time,String pattern) {
        DateTimeFormatter formatter2 = DateTimeFormatter.ofPattern(pattern);
        LocalDate parsedDate2 = LocalDate.parse(time, formatter2);
        return parsedDate2;
    }

    //获取指定时间的指定格式
    public static LocalDate formatTime2(String time) {
        return LocalDateTimeUtils.formatLocalDateTime(time,YYYYMMDDHHMMSS).toLocalDate();
    }

    //日期加上一个数,根据field不同加不同值,field为ChronoUnit.*
    public static LocalDateTime plus(LocalDateTime time, long number, TemporalUnit field) {
        return time.plus(number, field);
    }

    //日期减去一个数,根据field不同减不同值,field参数为ChronoUnit.*
    public static LocalDateTime minu(LocalDateTime time, long number, TemporalUnit field){
        return time.minus(number,field);
    }

    //获取上个月的今天
    public static String getLastMonth(String pattern) {
        LocalDateTime time=LocalDateTime.now().plusMonths(-1);
        return time.format(DateTimeFormatter.ofPattern(pattern));
    }

    /**
     * 获取两个日期的差  field参数为ChronoUnit.*
     * @param startTime
     * @param endTime
     * @param field  单位(年月日时分秒)
     * @return
     */
    public static long betweenTwoTime(LocalDateTime startTime, LocalDateTime endTime, ChronoUnit field) {
        Period period = Period.between(LocalDate.from(startTime), LocalDate.from(endTime));
        if (field == ChronoUnit.YEARS) return period.getYears();
        if (field == ChronoUnit.MONTHS) return period.getYears() * 12 + period.getMonths();
        return field.between(startTime, endTime);
    }

    /**
     * 获取两个日期的差  field参数为ChronoUnit.*
     * @param startTime
     * @param endTime
     * @param field  单位(年月日时分秒)
     * @return
     */
    public static long betweenTwoTime(LocalDate startTime, LocalDate endTime, ChronoUnit field) {
        Period period = Period.between(startTime, endTime);
        if (field == ChronoUnit.YEARS) return period.getYears();
        if (field == ChronoUnit.MONTHS) return period.getYears() * 12L + period.getMonths();
        return field.between(startTime, endTime)+1;
    }

    //获取一天的开始时间，2017,7,22 00:00
    public static LocalDateTime getDayStart(LocalDateTime time) {
        return time.withHour(0)
                .withMinute(0)
                .withSecond(0)
                .withNano(0);
    }

    //获取一天的结束时间，2017,7,22 23:59:59.999999999
    public static LocalDateTime getDayEnd(LocalDateTime time) {
        return time.withHour(23)
                .withMinute(59)
                .withSecond(59)
                .withNano(999999999);
    }


    public static boolean compareSmsDate(LocalDateTime cTime) {
        boolean f = false;
        LocalDateTime newDate = cTime.plusMinutes(1);
        LocalDateTime nowDate = LocalDateTime.now();
        if(nowDate.isBefore(newDate)){
            f = true;
        }
        return f;
    }

    /**
     * 一周的第一天
     *
     * @return {@link LocalDate}
     */
    public static LocalDate firstDayOfWeek(LocalDate now){
        if(now==null){
            now=LocalDate.now();
        }
        return now.with(DayOfWeek.MONDAY);
    }
    /**
     * 一周的最后一天
     *
     * @return {@link LocalDate}
     */
    public static LocalDate lastDayOfWeek(LocalDate now){
        if(now==null){
            now=LocalDate.now();
        }
        return now.with(DayOfWeek.SUNDAY);
    }

    /**
     * 月末的一天
     *
     * @return {@link LocalDate}
     */
    public static LocalDate lastDayOfMonth(LocalDate time){
        if(time == null){
            time = LocalDate.now();
        }
        return time.with(TemporalAdjusters.lastDayOfMonth());
    }

    /**
     * 月初的一天
     *
     * @return {@link LocalDate}
     */
    public static LocalDate firstDayOfMonth(LocalDate time){
        if(time == null){
            time = LocalDate.now();
        }
        return  time.with(TemporalAdjusters.firstDayOfMonth());
    }

    /**
     * 获取下个月的月份
     *
     * @return {@link LocalDate}
     */
    public static String getNextMonth(){
        LocalDate now=LocalDate.now();
        return now.plusMonths(1).format(DateTimeFormatter.ofPattern(YYYYMM));
    }
    /**
     * 获取上个月的月份
     *
     * @return {@link LocalDate}
     */
    public static String getLastMonth(){
        LocalDate now=LocalDate.now();
        return now.plusMonths(-1).format(DateTimeFormatter.ofPattern(YYYYMM));
    }

    public static String getMonth(LocalDate localDate){
        if(localDate == null) localDate = LocalDate.now();
        return localDate.format(DateTimeFormatter.ofPattern(YYYYMM));
    }
    /**
     * 获取下个月的年份
     *
     * @return {@link LocalDate}
     */
    public static String getNextYear(){
        LocalDate now=LocalDate.now();
        return now.plusYears(1).format(DateTimeFormatter.ofPattern(YYYY));
    }

    /**
     * 获取当前年份的最后一天
     *
     * @return {@link LocalDate}
     */
    public static LocalDate getLastDay(LocalDate localDate){
        if(localDate == null){
            localDate = LocalDate.now();
        }
        return LocalDate.of(localDate.getYear(),12,31);
    }

    /**
     * 获取当前年份的第一天
     *
     * @return {@link LocalDate}
     */
    public static LocalDate getFirstDay(LocalDate localDate){
        if(localDate == null){
            localDate = LocalDate.now();
        }
        return LocalDate.of(localDate.getYear(),1,1);
    }


    //获取传入日期月份的天数，日期格式: 2022-07-03
    public static int getDays(String month){
        String[] time =month.split("-");
        LocalDate date = LocalDate.of(Integer.parseInt(time[0]),Integer.parseInt(time[1]),1);
        return date.with(TemporalAdjusters.lastDayOfMonth()).getDayOfMonth();
    }

    /**
     * 获取当前日期所在季度的开始日期和结束日期
     * 季度一年四季， 第一季度：1月-3月， 第二季度：4月-6月， 第三季度：7月-9月， 第四季度：10月-12月
     * @param isFirst  true表示查询本季度开始日期  false表示查询本季度结束日期
     * @return
     */
    public static LocalDate getStartOrEndDayOfQuarter(LocalDate today,Boolean isFirst){
        LocalDate resDate = LocalDate.now();
        if (today == null) {
            today = resDate;
        }
        Month month = today.getMonth();
        Month firstMonthOfQuarter = month.firstMonthOfQuarter();
        Month endMonthOfQuarter = Month.of(firstMonthOfQuarter.getValue() + 2);
        if (isFirst) {
            resDate = LocalDate.of(today.getYear(), firstMonthOfQuarter, 1);
        } else {
            resDate = LocalDate.of(today.getYear(), endMonthOfQuarter, endMonthOfQuarter.length(today.isLeapYear()));
        }
        return resDate;
    }

    /**
     * 获取本月最后一天
     * @param s
     * @return
     */
    public static LocalDate getMonthLastDay(String s) {
        if(s==null||s.equals("")){
            return null;
        }
        if(s.length()>7){
            s= s.substring(0,7);
        }
        LocalDate date = LocalDate.parse(s+"-01", DateTimeFormatter.ofPattern("yyyy-MM-dd"));
        return date.with(TemporalAdjusters.lastDayOfMonth());
    }



    /**
     * 获取两个日期中间的天,若超过一年只算到年尾
     * @param createDate
     * @param now
     * @return
     */
    public static List<LocalDate> getDayToDay(LocalDate createDate, LocalDate now) {
        if(now.getYear()>createDate.getYear()){
            //去年的最后一天
            LocalDateTime lastDay = LocalDateTime.now().plusYears(-1).with(TemporalAdjusters.lastDayOfYear()).withHour(23).withMinute(59).withSecond(59);
            now = lastDay.toLocalDate();
        }
        long numOfDaysBetween = ChronoUnit.DAYS.between(createDate, now);
        return IntStream.iterate(0, i -> i + 1)
                .limit(numOfDaysBetween)
                .mapToObj(createDate::plusDays)
                .collect(Collectors.toList());
    }
    private static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    /**
     * 遍历两个日期之间的月
     * @param startDateStr   开始日期字符串
     * @param endDateStr     结束字符串
     */
    public static List<String> getMonthToMonth(String startDateStr, String endDateStr) throws ParseException {
        List<String> list = new ArrayList<>();
        //将字符串类型的日期转换为Date类型并加载到Calendar中(有异常直接抛)
        Date startDate = sdf.parse(startDateStr);
        Date endDate = sdf.parse(endDateStr);
        int startYear = Integer.parseInt(new SimpleDateFormat("yyyy").format(startDate));
        int endYear = Integer.parseInt(new SimpleDateFormat("yyyy").format(endDate));
        if(endYear>startYear){
            //若超过一年则获取去年的最后一天
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
            endDate =  format.parse(startYear+"-12-31");
        }
        //实例化Calendar(为什么要实例化两个Calendar，肯定是为了后面要用啊0.0)
        Calendar startCalendar = Calendar.getInstance();
        Calendar endCalendar = Calendar.getInstance();
        //将两个日期加载到Calendar中
        startCalendar.setTime(startDate);
        endCalendar.setTime(endDate);
        //开始遍历
        while(true){
            //和按日输出差不多
            String currentDateStr = sdf.format(startCalendar.getTime()).substring(0,7);
            list.add(currentDateStr);
            //日期加1
            startCalendar.add(Calendar.MONTH, 1);

            //当前日期和结束日历日期比较，超过结束日期则终止
            if(startCalendar.after(endCalendar))break;
        }

        return list;
    }

    /**
     * 遍历两个日期之间的周
     * @param startDateStr   开始日期字符串
     * @param endDateStr     结束字符串
     */
    public static List<HashMap<LocalDate,LocalDate>> getWeekToWeek(String startDateStr, String endDateStr) throws ParseException {
        List<HashMap<LocalDate,LocalDate>> list = new ArrayList<>();
        //将字符串类型的日期转换为Date类型并加载到Calendar中(有异常直接抛)

        Date startDate = sdf.parse(startDateStr);
        Date endDate = sdf.parse(endDateStr);
        Date yearLast =sdf.parse(startDateStr.substring(0,4)+"-12-31");
        //实例化Calendar(为什么要实例化两个Calendar，肯定是为了后面要用啊0.0)
        Calendar startCalendar = Calendar.getInstance();
        Calendar endCalendar = Calendar.getInstance();
        //将两个日期加载到Calendar中
        startCalendar.setTime(startDate);
        endCalendar.setTime(endDate);
        //按周输出
        while(true){
            String firstAndSecondStr = "";
            String secondDateStr = "";
            String firstDateStr = sdf.format(startCalendar.getTime());
            startCalendar.add(Calendar.DAY_OF_MONTH, 6);
            secondDateStr = sdf.format(startCalendar.getTime());
            if (startCalendar.after(endCalendar)){
                startCalendar.add(Calendar.DAY_OF_MONTH, -6);
                if(sdf.format(startCalendar.getTime()).equals(endDateStr)){
                    firstAndSecondStr = endDateStr + "~" + endDateStr;
                    break;
                }
                else{
                    firstDateStr = sdf.format(startCalendar.getTime());
                    firstAndSecondStr = firstDateStr + "~" + endDateStr;
                    break;
                }
            };
            HashMap<LocalDate, LocalDate> localDateLocalDateHashMap = new HashMap<>();
            localDateLocalDateHashMap.put(LocalDate.parse(firstDateStr, DateTimeFormatter.ofPattern("yyyy-MM-dd")),
                    LocalDate.parse(secondDateStr, DateTimeFormatter.ofPattern("yyyy-MM-dd")));
            list.add(localDateLocalDateHashMap);
            startCalendar.add(Calendar.DAY_OF_MONTH, 1);
            if (startCalendar.equals(endCalendar)){
//                        resultMap.put(sdf.format(startCalendar.getTime()) + "~" + endDateStr, null);
                break;
            }
            if (startCalendar.after(endCalendar))
                break;
        }
        return list;
    }

    /**
     * 获取时间之间的季度
      * @param format
     * @param format1
     * @return
     */
    public static List<String> getThreeMonth(String format, String format1) throws ParseException {
        HashSet<String> set = new HashSet<>();
        List<String> monthToMonth = getMonthToMonth(format, format1);
        for (String s : monthToMonth) {
            if(s.contains("-01")){
                String s1 = s.replace("-01", "-01") + "-01";
                LocalDate localDate = LocalDate.parse(s1, DateTimeFormatter.ofPattern("yyyy-MM-dd")).plusMonths(3);
                if(!LocalDate.now().isBefore(localDate)){
                    set.add(s.replace("-01","-01"));
                }

            }
            if(s.contains("-02")){
                String s1 = s.replace("-02", "-01") + "-01";
                LocalDate localDate = LocalDate.parse(s1, DateTimeFormatter.ofPattern("yyyy-MM-dd")).plusMonths(3);
                if(!LocalDate.now().isBefore(localDate)){
                    set.add(s.replace("-02","-01"));
                }

            }if(s.contains("-03")){
                String s1 = s.replace("-03","-01") + "-01";
                LocalDate localDate = LocalDate.parse(s1, DateTimeFormatter.ofPattern("yyyy-MM-dd")).plusMonths(3);
                if(!LocalDate.now().isBefore(localDate)){
                    set.add(s.replace("-03","-01"));
                }
            }
            if(s.contains("-04")){
                String s1 = s.replace("-04", "-01") + "-01";
                LocalDate localDate = LocalDate.parse(s1, DateTimeFormatter.ofPattern("yyyy-MM-dd")).plusMonths(3);
                if(!LocalDate.now().isBefore(localDate)){
                    set.add(s.replace("-04","-01"));
                }

            }if(s.contains("-05")){
                String s1 = s.replace("-05","-04") + "-01";
                LocalDate localDate = LocalDate.parse(s1, DateTimeFormatter.ofPattern("yyyy-MM-dd")).plusMonths(3);
                if(!LocalDate.now().isBefore(localDate)){
                    set.add(s.replace("-05","-01"));
                }
            }if(s.contains("-06")){
                String s1 = s.replace("-06","-01") + "-01";
                LocalDate localDate = LocalDate.parse(s1, DateTimeFormatter.ofPattern("yyyy-MM-dd")).plusMonths(3);
                if(!LocalDate.now().isBefore(localDate)){
                    set.add(s.replace("-06","-01"));
                }
            } if(s.contains("-07")){
                String s1 = s.replace("-07", "-01") + "-01";
                LocalDate localDate = LocalDate.parse(s1, DateTimeFormatter.ofPattern("yyyy-MM-dd")).plusMonths(3);
                if(!LocalDate.now().isBefore(localDate)){
                    set.add(s.replace("-07","-01"));
                }

            }
            if(s.contains("-08")){
                String s1 = s.replace("-08","-01") + "-01";
                LocalDate localDate = LocalDate.parse(s1, DateTimeFormatter.ofPattern("yyyy-MM-dd")).plusMonths(3);
                if(!LocalDate.now().isBefore(localDate)){
                    set.add(s.replace("-08","-01"));
                }
            }if(s.contains("-09")){
                String s1 = s.replace("-09","-01") + "-01";
                LocalDate localDate = LocalDate.parse(s1, DateTimeFormatter.ofPattern("yyyy-MM-dd")).plusMonths(3);
                if(!LocalDate.now().isBefore(localDate)){
                    set.add(s.replace("-09","-01"));
                }
            }
            if(s.contains("-10")){
                String s1 = s.replace("-10", "-01") + "-01";
                LocalDate localDate = LocalDate.parse(s1, DateTimeFormatter.ofPattern("yyyy-MM-dd")).plusMonths(3);
                if(!LocalDate.now().isBefore(localDate)){
                    set.add(s.replace("-10","-01"));
                }

            }if(s.contains("-11")){
                String s1 = s.replace("-11","-01") + "-01";
                LocalDate localDate = LocalDate.parse(s1, DateTimeFormatter.ofPattern("yyyy-MM-dd")).plusMonths(3);
                if(!LocalDate.now().isBefore(localDate)){
                    set.add(s.replace("-11","-01"));
                }
            }if(s.contains("-12")){
                String s1 = s.replace("-12","-01") + "-01";
                LocalDate localDate = LocalDate.parse(s1, DateTimeFormatter.ofPattern("yyyy-MM-dd")).plusMonths(3);
                if(!LocalDate.now().isBefore(localDate)){
                    set.add(s.replace("-12","-01"));
                }
            }
        }
        monthToMonth = new ArrayList<>(set);
        return monthToMonth;
    }

    /**
     * 获取时间之间的半年
     * @param format
     * @param format1
     * @return
     */
    public static List<String> getSexMonth(String format, String format1) throws ParseException {
        List<String> monthToMonth = getMonthToMonth(format, format1);
        HashSet<String> set = new HashSet<String>();
        for (String s : monthToMonth) {
            LocalDate parse = LocalDate.parse(s + "-01", DateTimeFormatter.ofPattern("yyyy-MM-dd"));
            if(parse.getMonthValue()<=6){
                //判断是否过完0.5年
                LocalDate nextYear = parse.plusMonths(6);
                if(!LocalDate.now().isBefore(nextYear)){
                    set.add(parse.getYear()+"-01");
                }

            }else{
                LocalDate nextYear = parse.plusMonths(6);
                if(!LocalDate.now().isBefore(nextYear)){
                    set.add(parse.getYear()+"-07");
                }
            }
        }
        return new ArrayList<>(set);
    }

    /**
     * 获取时间之间的一年
     * @param format
     * @param format1
     * @return
     */
    public static List<String> getYearMonth(String format, String format1) throws ParseException {
        List<String> monthToMonth = getMonthToMonth(format, format1);
        HashSet<String> set = new HashSet<String>();
        for (String s : monthToMonth) {
            LocalDate parse = LocalDate.parse(s + "-01", DateTimeFormatter.ofPattern("yyyy-MM-dd"));
            //判断是否过完一年
            LocalDate nextYear = parse.plusYears(1);
            if(!LocalDate.now().isBefore(nextYear)){
                set.add(parse.getYear()+"-01");
            }
        }
        return new ArrayList<>(set);
    }

    /**
     * 获取x个月后的时间
     * 且不能超过一年
     * @param parse
     * @return
     */
    public static LocalDate getLastXMonth(LocalDate parse,int x) {
        LocalDate localDate = parse.plusMonths(x);
        return localDate;
    }

    /**
     * 获取年月
     * @param parse
     * @return
     */
    public static String getYearMonth(String parse) {
        if(parse.length()>10){
            return LocalDateTime.parse(parse, DateTimeFormatter.ofPattern(YYYYMMDDHHMMSS)).format(DateTimeFormatter.ofPattern(YYYYMM));
        }else{
            return LocalDate.parse(parse, DateTimeFormatter.ofPattern(YYYYMMDD)).format(DateTimeFormatter.ofPattern(YYYYMM));
        }

    }

    /**
     * 比量两个月份的大小
     * @param date1
     * @param date2
     * @return
     * 0:相等
     * -1：检查 date1 是否在 date2 之前
     *  1：检查 date1 是否在 date2 之后
     */
    public static int compareMonth(String date1,String date2) {
        LocalDate firstMonthDay =  formatLocalDate(date1+"-01");
        LocalDate lastMonthDay  =  formatLocalDate(date2+"-01");
        if (firstMonthDay.equals(lastMonthDay)) {
            return 0;
        }
        if (firstMonthDay.isBefore(lastMonthDay)) {
            return -1;
        }
        if (firstMonthDay.isAfter(lastMonthDay)) {
            return 1;
        }
        return 0;
    }

    public static LocalDate getFirstDayOfWeek(LocalDate day){
        if(day == null){
            day = LocalDate.now();
        }
        // 计算本周第一天（通常是周一）的日期
        return day.with(TemporalAdjusters.previousOrSame(DayOfWeek.MONDAY));
    }

    public static LocalDate getLastDayOfWeek(LocalDate day){
        if(day == null){
            day = LocalDate.now();
        }
        // 计算本周第一天（通常是周一）的日期
        return day.with(TemporalAdjusters.nextOrSame(DayOfWeek.SUNDAY));
    }

    public static void main(String[] args) {
        System.err.println(getLastDayOfWeek(LocalDate.now()));
    }

}
